package com.turnengine.client.local.unit.list;

import static com.javabi.common.lang.Variables.isNull;
import static com.javabi.common.lang.Variables.notNull;
import static com.turnengine.client.local.unit.enums.UnitType.LOCATION;
import static com.turnengine.client.local.unit.enums.UnitType.PLAYER;

import com.turnengine.client.local.unit.enums.UnitType;

public class UnitListSet implements IUnitListSet {

	private final UnitType type;
	private final IUnitList playerUnitList;
	private final IUnitList locationUnitList;
	private final IUnitList mobileUnitList;

	public UnitListSet(UnitType type, IUnitList player, IUnitList location, IUnitList mobile) {
		this.type = notNull("type", type);

		// Player units (mandatory)
		this.playerUnitList = notNull("player", player);

		// Location units?
		if (type.equals(UnitType.LOCATION)) {
			this.locationUnitList = notNull("location", location);
			this.mobileUnitList = notNull("mobile", mobile);
		}

		// Mobile units?
		else {
			this.locationUnitList = isNull("location", location);
			this.mobileUnitList = notNull("mobile", mobile);
		}
	}

	@Override
	public UnitType getUnitType() {
		return type;
	}

	@Override
	public IUnitList getPlayerUnitList() {
		return playerUnitList;
	}

	@Override
	public IUnitList getLocationUnitList() {
		return locationUnitList;
	}

	@Override
	public IUnitList getMobileUnitList() {
		return mobileUnitList;
	}

	@Override
	public IUnitList getUnitList(UnitType type) {
		if (type.equals(PLAYER)) {
			return playerUnitList;
		}

		// Location type
		if (type.equals(LOCATION)) {
			if (locationUnitList == null) {
				throw new IllegalArgumentException("location unit list not set for type: " + getUnitType());
			}
			return locationUnitList;
		}

		// Mobile type
		else {
			if (mobileUnitList == null) {
				throw new IllegalArgumentException("mobile unit list not set for type: " + getUnitType());
			}
			return mobileUnitList;
		}
	}

}
